package message.test.rest;

import io.swagger.annotations.*;
import message.test.domain.Demo;
import message.test.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

/**
 * .
 *
 * @author sunhao(sunhao.java @ gmail.com)
 * @version V1.0, 16/1/18 下午6:42
 */
@Api(value = "Mybatis的CRUD测试", description = "Mybatis的CRUD测试")
@RestController
@RequestMapping("/demo")
public class DemoController {
    @Autowired
    private DemoService demoService;

    @ApiOperation(value = "保存demo到Test库", notes = "保存demo到Test库")
    @ApiResponses({@ApiResponse(code = 200, message = "保存demo成功")})
    @RequestMapping(value = "/saveIntoTest", method = RequestMethod.POST)
    public boolean saveIntoTest(@ApiParam(name = "demoDto", value = "demo") @RequestBody Demo demo) {
        return demoService.insertIntoTest(demo) == 1;
    }

    @ApiOperation(value = "保存demo到Demo库", notes = "保存demo到Demo库")
    @ApiResponses({@ApiResponse(code = 200, message = "保存demo成功")})
    @RequestMapping(value = "/saveIntoDemo", method = RequestMethod.POST)
    public boolean saveIntoDemo(@ApiParam(name = "demoDto", value = "demo") @RequestBody Demo demo) {
        return demoService.insertIntoDemo(demo) == 1;
    }

    @ApiOperation(value = "获取Demo库所有demo", notes = "获取所有demo")
    @ApiResponses({@ApiResponse(code = 200, message = "获取所有demo成功")})
    @RequestMapping(value = "/listFromDemo", method = RequestMethod.GET)
    public Page<Demo> list1(@ApiParam(name = "pageable", value = "分页信息,传参方式：?page=0&size=10") @PageableDefault(page = 0, size = 10) Pageable pageable) {
        return demoService.listFromDemo(pageable);
    }

    @ApiOperation(value = "获取Test库所有demo", notes = "获取所有demo")
    @ApiResponses({@ApiResponse(code = 200, message = "获取所有demo成功")})
    @RequestMapping(value = "/listFromTest", method = RequestMethod.GET)
    public Page<Demo> list2(@ApiParam(name = "pageable", value = "分页信息,传参方式：?page=0&size=10") @PageableDefault(page = 0, size = 10) Pageable pageable) {
        return demoService.listFromTest(pageable);
    }

    @ApiOperation(value = "获取Demo库demo", notes = "获取Demo库demo")
    @ApiResponses({@ApiResponse(code = 200, message = "获取demo成功")})
    @RequestMapping(value = "/demo/{id}", method = RequestMethod.GET)
    public Demo loadFromDemo(@PathVariable("id") Long id) {
        return demoService.selectFromDemo(id);
    }

    @ApiOperation(value = "获取Test库demo", notes = "获取Test库demo")
    @ApiResponses({@ApiResponse(code = 200, message = "获取demo成功")})
    @RequestMapping(value = "/test/{id}", method = RequestMethod.GET)
    public Demo loadFromTest(@PathVariable("id") Long id) {
        return demoService.selectFromTest(id);
    }

    @ApiOperation(value = "测试void", notes = "测试void")
    @ApiResponses({@ApiResponse(code = 200, message = "测试void成功")})
    @RequestMapping(value = "/void", method = RequestMethod.GET)
    public void testForVoid() {
        System.out.println("void");
    }

    @ApiOperation(value = "测试demo的事务", notes = "测试demo的事务")
    @ApiResponses({@ApiResponse(code = 200, message = "测试demo的事务成功")})
    @RequestMapping(value = "/demo/transactional", method = RequestMethod.POST)
    public void demoTransactional() {
        demoService.demoTransactional();
    }

    @ApiOperation(value = "测试test的事务", notes = "测试test的事务")
    @ApiResponses({@ApiResponse(code = 200, message = "测试test的事务成功")})
    @RequestMapping(value = "/test/transactional", method = RequestMethod.POST)
    public void testTransactional() {
        demoService.testTransactional();
    }
}
